//
//  CountryCodeTVC.m
//  EPTool
//
//  Created by guoshiqing on 2018/5/2.
//  Copyright © 2017年 云数动能. All rights reserved.
//

#import "CountryCodeTVC.h"
#import "CountryCode.h"
#import "NSString+Additions.h"
#import "SpellLibrary.h"
#import "Macro.h"
#import "SectionHeaderView.h"
#import "BottomTopLineTableViewCell.h"


@interface CountryCodeTVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation CountryCodeTVC


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"选择国家和地区";
    
    // 设置导航栏背景色不透明
    self.navigationController.navigationBar.translucent = NO;
    
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

// 修改状态栏文字颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - 懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        
        CGFloat height = UI_SCREEN_HEIGHT - NavBar_Height;
        if ([self.navigationController superclass] != [UINavigationController class]) {
            height = UI_SCREEN_HEIGHT;
        }
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, height)];
        _tableView.separatorStyle = UITableViewCellEditingStyleNone;
    
        if (@available(iOS 11.0, *))
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    return _tableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dicCode.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = self.dicCode[[[self.dicCode allKeys] objectAtIndex:section]];
    return array.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}



- (BottomTopLineTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"Cell";
    BottomTopLineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[BottomTopLineTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }

    NSString *key = [[self.dicCode allKeys] objectAtIndex:indexPath.section];
    NSArray *array = [self.dicCode objectForKey:key];
    CountryModel *c = array[indexPath.row];
    
    cell.textLabel.text = c.name;
    cell.detailTextLabel.text = c.dial_code;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = [[self.dicCode allKeys] objectAtIndex:indexPath.section];
    NSArray *array = self.dicCode[key];
    CountryModel *c = array[indexPath.row];
    
    __weak typeof(self) weakSelf = self;
    if (nil != self.completion) {
        weakSelf.completion(c);
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }
}

@end
